package Plugins::MLBGameday::Util;

# Copyright Ted Romer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

use strict;
use warnings;

use Exporter::Lite;
our @EXPORT = qw(gameDisplayLine reloadOnChange);

use Module::Reload;

sub gameDisplayLine {
    my $game = shift;

    my $displayLine;

    my @local = localtime($game->start_time());
    my $lhour = $local[2];
    my $lmin = $local[1];
    my $lampm;
    if ( $lhour >= 12 ) {
        $lampm = 'pm';
    } else {
        $lampm = 'am';
    }
    $lhour = ($lhour + 11) % 12 + 1;

    my $away_score = $game->away_score();
    my $home_score = $game->home_score();
    my $inning = $game->inning();

    my $status = $game->status();
    if ( $status eq 'PRE_GAME' || !$inning ) {
        $displayLine =
            $game->away()->nickname() .
            ' at ' .
            $game->home()->nickname() .
            sprintf " %d:%02d %s", $lhour, $lmin, $lampm;
    }
    elsif ( $status eq 'OTHER' ) {
        $displayLine =
            $game->away()->nickname() .
            ' at ' .
            $game->home()->nickname() .
            ' ' . $game->delay_reason();
    }
    else {
        $displayLine =
            $game->away()->nickname() . " $away_score at " .
            $game->home()->nickname() . " $home_score";
        if ( $game->status() =~ m/In Progress/i ) {
            if ( $game->top_inning() =~ m/^TY/i ) {
                $displayLine .= ' T';
            }
            else {
                $displayLine .= ' B';
            }
            $displayLine .= " $inning";
            $displayLine .=
                ( ($inning % 10) == 1 ? 'st' :
                  ($inning % 10) == 2 ? 'nd' :
                  ($inning % 10) == 3 ? 'rd' :
                  'th' );
        }
        elsif ( $game->status() =~ m/Final/i ) {
            $displayLine .= ' F';
        }
    }

    return $displayLine;
}

#my $reloadOnChangeFlag = 0;
#if ( -e "/tmp/reload-MLBGameday-on-change" ) {
#    $reloadOnChange = 1;
#}
sub reloadOnChange {

    if ( -e "/tmp/reload-MLBGameday-on-change" ) {
        Module::Reload->check;
    }
    return;
}

1;

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
